/*
 * Sylvain BERTRAND <digital.ragnarok@gmail.com>
 * code protected by GNU GPL v3
 */
#include <X11/Xlib.h>
#include <X11/extensions/sync.h>
#include <glib.h>
#include <libxml/xinclude.h>
#include "../../toolkit/keyboard.h"
#include "../../toolkit/xml.h"
#include "../../toolkit/prop.h"
#include "../misc.h"
#include "../geom.h"
#include "../frame.h"
#include "../actions.h"
#include "../window.h"
#include "../stacking.h"
#include "../client.h"
#include "../moveresize.h"
struct options {
	gboolean type_specified;
	enum wm_moveresize_type type;
};

static enum wm_moveresize_type type_compute(gint x, gint y, gint cx, gint cy, gint cw, gint ch)
{
	x = x - cx;
	y = ch - (y - cy);
#define X x*ch/cw
#define A -4*X + 7*ch/3
#define B  4*X -15*ch/9
#define C -X/4 + 2*ch/3
#define D  X/4 + 5*ch/12
#define E  X/4 +   ch/3
#define F -X/4 + 7*ch/12
#define G  4*X - 4*ch/3
#define H -4*X + 8*ch/3
#define a (y > 5*ch/9)
#define b (x < 4*cw/9)
#define c (x > 5*cw/9)
#define d (y < 4*ch/9)
	if(y < A && y >= C)
		return WM_SIZE_TOPLEFT;
	else if(y >= A && y >= B && a)
		return WM_SIZE_TOP;
	else if(y < B && y >= D)
		return WM_SIZE_TOPRIGHT;
	else if(y < C && y >= E && b)
		return WM_SIZE_LEFT;
	else if(y < D && y >= F && c)
		return WM_SIZE_RIGHT;
	else if(y < E && y >= G)
		return WM_SIZE_BOTTOMLEFT;
	else if(y < G && y < H && d)
		return WM_SIZE_BOTTOM;
	else if(y >= H && y < F)
		return WM_SIZE_BOTTOMRIGHT;
	else
		return WM_MOVE;
#undef X
#undef A
#undef B
#undef C
#undef D
#undef E
#undef F
#undef G
#undef H
#undef a
#undef b
#undef c
#undef d
}

static gpointer setup_func(xmlNodePtr node)
{
	xmlNodePtr n;
	struct options *o;
	o = g_slice_new0(struct options);
	if((n = t_xml_find_node(node, "edge"))) {
		gchar *s = t_xml_node_string(n);
		o->type_specified = TRUE;
		if(!g_ascii_strcasecmp(s, "top"))
			o->type = WM_SIZE_TOP;
		else if(!g_ascii_strcasecmp(s, "bottom"))
			o->type = WM_SIZE_BOTTOM;
		else if(!g_ascii_strcasecmp(s, "left"))
			o->type = WM_SIZE_LEFT;
		else if(!g_ascii_strcasecmp(s, "right"))
			o->type = WM_SIZE_RIGHT;
		else if(!g_ascii_strcasecmp(s, "topleft"))
			o->type = WM_SIZE_TOPLEFT;
		else if(!g_ascii_strcasecmp(s, "topright"))
			o->type = WM_SIZE_TOPRIGHT;
		else if(!g_ascii_strcasecmp(s, "bottomleft"))
			o->type = WM_SIZE_BOTTOMLEFT;
		else if(!g_ascii_strcasecmp(s, "bottomright"))
			o->type = WM_SIZE_BOTTOMRIGHT;
		else
			o->type_specified = FALSE;
		g_free(s);
	}
	return o;
}

static void free_func(gpointer o)
{
	g_slice_free(struct options, o);
}

static gboolean run_func(struct wm_actions_data *data, gpointer options)
{
	struct options *o = options;
	if(data->client) {
		struct wm_client *c = data->client;
		enum wm_moveresize_type type;
		if(!data->button)
			type = WM_SIZE_KEYBOARD;
		else if(o->type_specified)
			type = o->type;
		else
			type = type_compute(data->x, data->y, c->frame->area.x, c->frame->area.y,
				c->area.width + c->frame->size.left + c->frame->size.right,
				c->area.height + c->frame->size.top + c->frame->size.bottom);
		moveresize_start(c, data->x, data->y, data->button, type);
	}
	return FALSE;
}

void action_resize_startup(void)
{
	actions_register("Resize", setup_func, free_func, run_func);
}
